package fr.pud.client.view;

import java.awt.Toolkit;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import fr.pud.client.controller.query.ApplyModificationQueries;
import fr.pud.client.controller.query.CheckAddingQuery;
import fr.pud.client.controller.query.CloseQueryFrame;
import fr.pud.client.controller.query.DeleteQueryFromQueries;
import fr.pud.client.controller.query.ModifyQueryFromQueries;
import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.model.Queries;
import fr.pud.client.model.Query;
import fr.pud.client.view.jsuggestfield.JSuggestField;
import fr.pud.utils.Tools;

public class QueryFrame extends JFrame {
    private static final long serialVersionUID = 1618553482135961463L;

    public static QueryFrame getInstance() {
        initialize();
        return instance;
    }

    private static void initialize() {
        if (instance == null) {
            instance = new QueryFrame();
        }
    }

    private JPanel               panelSouth;
    private SummarizeInfo<Query> listQueries;
    private InputQueryDataPanel  dataPanel;
    private JButton              buttonValidateClose;
    private JButton              buttonAdd;
    private JButton              buttonModify;
    private JButton              buttonApplyModification;
    private JButton              buttonDelete;
    private static QueryFrame    instance;

    public QueryFrame() {
        this.setTitle(LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.ADD_QUERY));
        this.instancePanels();
        this.instanceButtons();
        this.addPanels();
        this.addButtons();
        this.setVisible(true);
        this.getContentPane().setLayout(
                new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        this.pack();
        this.placeFrame();
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    private void addButtons() {
        this.panelSouth.add(this.buttonAdd);
        this.panelSouth.add(this.buttonValidateClose);
        this.panelSouth.add(this.buttonModify);
        this.buttonApplyModification.setVisible(false);
        this.panelSouth.add(this.buttonApplyModification);
        this.panelSouth.add(this.buttonDelete);
    }

    private void addPanels() {
        JScrollPane scroll = new JScrollPane(this.listQueries);
        this.getContentPane().add(scroll);
        this.getContentPane().add(this.dataPanel);
        this.getContentPane().add(this.panelSouth);
    }

    /*
     * (non-Javadoc)
     * @see java.awt.Window#dispose()
     */
    @Override
    public void dispose() {
        MainFrame.getSingleton().setVisible(true);
        MainFrame.getSingleton().setEnabled(true);
        super.dispose();
    }

    /**
     * @return the buttonAdd
     */
    public JButton getButtonAdd() {
        return this.buttonAdd;
    }

    /**
     * @return the buttonApplyModification
     */
    public JButton getButtonApplyModification() {
        return this.buttonApplyModification;
    }

    /**
     * @return the buttonSelete
     */
    public JButton getButtonDelete() {
        return this.buttonDelete;
    }

    /**
     * @return the buttonModify
     */
    public JButton getButtonModify() {
        return this.buttonModify;
    }

    /**
     * @return the buttonValidateClose
     */
    public JButton getButtonValidateClose() {
        return this.buttonValidateClose;
    }

    public InputQueryDataPanel getDataPanel() {
        initialize();
        return this.dataPanel;
    }

    /**
     * @return the listQueries
     */
    public SummarizeInfo<Query> getListQueries() {
        initialize();
        return this.listQueries;
    }

    private void instanceButtons() {
        this.buttonAdd = new JButton(new CheckAddingQuery());
        this.buttonValidateClose = new JButton(new CloseQueryFrame());
        this.buttonModify = new JButton(new ModifyQueryFromQueries());
        this.buttonApplyModification = new JButton(
                new ApplyModificationQueries());
        this.buttonDelete = new JButton(new DeleteQueryFromQueries());
    }

    private void instancePanels() {
        JSuggestField[] arrayJsuggestfield = {
                new JSuggestField(this, Tools.TIMES_DATABASE),
                new JSuggestField(this, Tools.TIMES_DATABASE),
                new JSuggestField(this, Tools.TIMES_DATABASE),
                new JSuggestField(this, Tools.TIMES_DATABASE)
        };
        this.dataPanel = new InputQueryDataPanel(arrayJsuggestfield);
        this.panelSouth = new JPanel();
        this.listQueries = new SummarizeInfo<Query>(Queries.getInstance()
                .getQueries(), Queries.getInstance());
    }

    /**
     * Place the frame
     */
    private void placeFrame() {
        this.setLocation((int) Toolkit.getDefaultToolkit().getScreenSize()
                .getWidth()
                / 2 - this.getWidth() / 2, (int) Toolkit.getDefaultToolkit()
                .getScreenSize().getHeight()
                / 2 - this.getHeight());
    }
}
